﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System;
using DotNetUI.Models;
using System.Windows.Media;

namespace DotNetUI.Controls
{

    public  class DotNetTextBox : TextBox
    {
        static DotNetTextBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DotNetTextBox), new FrameworkPropertyMetadata(typeof(DotNetTextBox)));
        }
        public static readonly DependencyProperty PlaceHolderProperty = DependencyProperty.Register(nameof(PlaceHolder),typeof(object),typeof(DotNetTextBox));
        /// <summary>
        /// 默认放置内容
        /// </summary>
        public object PlaceHolder { get=>GetValue(PlaceHolderProperty); set=>SetValue(PlaceHolderProperty,value); }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            var button=(DotNetButton)  this.Template.FindName("button", this);
            button.Click += Button_Click;
            this.MouseWheel += DotNetTextBox_MouseWheel;
        }

        private void DotNetTextBox_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (double.TryParse(this.Text,out var m))
            {
                if (e.Delta>0)
                {
                    this.Text = (m + Step).ToString();
                }
                else
                {
                    this.Text = (m - Step).ToString();
                }
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Clear();
        }
        public static readonly DependencyProperty IconProperty = DependencyProperty.Register(nameof(Icon),typeof(GeometryDrawing),typeof(DotNetTextBox));
        public GeometryDrawing Icon { get=>this.GetValue<GeometryDrawing>(IconProperty); set=>SetValue(IconProperty,value); }
        /// <summary>
        /// 显示清空按钮
        /// </summary>
        public static readonly DependencyProperty ShowClearButtonProperty = DependencyProperty.Register(nameof(ShowClearButton),typeof(bool),typeof(DotNetTextBox));
        /// <summary>
        /// 显示清空按钮
        /// </summary>
        public bool ShowClearButton { get =>(bool) GetValue(ShowClearButtonProperty); set => SetValue(ShowClearButtonProperty, value); }

        /// <summary>
        /// 步进值
        /// </summary>
        public double Step { get; set; } = 1;
    }
    
}
